﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace FreeBroker
{
    public partial class BrokerMainForm : Form
    {
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BrokerObject obj = (BrokerObject)e.Argument;
            if (obj.IdNotation != null && obj.IdNotation.Length > 0)
            {
                try
                {
                    Communication com = new Communication();
                    obj.chartOptions[ChartController.OPT_TIMEPERIOD] = "3m";
                    obj.chartOptions[ChartController.OPT_CHARTTYPE] = "candle";
                    string request = ChartController.buildRequest(obj);
                    Image image  = com.comdirectImage(request, "");
                    Size currentThumbSize = GetCurrentThumbSize();
                    obj.chartThumbnail = ResizeImage(image, currentThumbSize.Width, currentThumbSize.Height, false);
                    if (obj.Url == null)
                    {
                        obj.Url = "http://www.comdirect.de/inf/ewf/redirect.html?ID_NOTATION=" + obj.IdNotation;
                    }
                    CollectAdditionalInfo(obj);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            e.Result = obj;
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BrokerObject obj = (BrokerObject)e.Result;
            if (obj != null)
            {
                Thread.Sleep(1000);
                SetAttribs(obj);
            }
            Console.WriteLine("Unlocking thread...");
            locked.Set();
        }

        public Image ResizeImage(Image FullsizeImage, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            return NewImage;
        }

    }
    
}
